package com.efast.dmom.equipment.service.repair.impl;

import java.util.List;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.equipment.bean.EquipRepairOrderDetailsBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.equipment.bean.EquipRepairOrderBean;
import com.efast.dmom.equipment.dao.repair.EquipRepairSpareConsumptionDao;
import com.efast.dmom.equipment.dao.repair.EquipRepairInfoDao;
import com.efast.dmom.equipment.dao.repair.EquipRepairOrderDao;
import com.efast.dmom.equipment.entity.EquipRepairInfo;
import com.efast.dmom.equipment.entity.EquipRepairOrder;
import com.efast.dmom.equipment.entity.EquipSpareConsumption;
import com.efast.dmom.equipment.service.repair.IEquipRepairOrderService;

@Service
public class EquipRepairOrderServiceImpl implements IEquipRepairOrderService {

	@Autowired
	private EquipRepairInfoDao repairInfoDao;

	@Autowired
	private EquipRepairSpareConsumptionDao spareConsumptionDao;

	@Autowired
	private EquipRepairOrderDao repairOrderDao;
	@Override
	public void saveRepairInfo(List<EquipRepairInfo> repairInfo) {

		repairInfoDao.saveRepairInfo(repairInfo);
	}

	@Override
	public void saveRepairSpare(List<EquipSpareConsumption> spareConsumption) {

		spareConsumptionDao.saveRepairSpareConsumption(spareConsumption);
	}

	@Override
	public void deleteRepairOrder(Integer id) {
		if(id>0) {
			repairOrderDao.deleteRepairOrder(id);
		}
	}

	@Override
	public void editRepairOrderDetail(EquipRepairOrderDetailsBean bean) {
		//删除维修信息
		repairInfoDao.deleteEquipRepairInfoByOrderId(bean.getOrderId());
		//删除维修备件信息
		spareConsumptionDao.deleteEquipSpareConsumptionByOrderId(bean.getOrderId());
		List<EquipRepairInfo> repairInfo = bean.getRepairInfoList();
		List<EquipSpareConsumption> consumption = bean.getSpareList();
		//保存维修信息
		if(repairInfo!=null&&repairInfo.size()>0){
			for(EquipRepairInfo e:repairInfo){
				e.setOperators(SpringWebUtils.getRequestUser());
				e.setOrderId(bean.getOrderId());
				e.setCompanyCode(SpringWebUtils.getRequestCompany());
				e.setCreateUser(SpringWebUtils.getRequestUser());
			}
		    repairInfoDao.saveRepairInfo(repairInfo);
		}
		//保养维修备件信息
		if(consumption!=null&&consumption.size()>0){
			for(EquipSpareConsumption e:consumption){
				e.setOrderId(bean.getOrderId());
				e.setCompanyCode(SpringWebUtils.getRequestCompany());
				e.setCreateUser(SpringWebUtils.getRequestUser());
			}
			spareConsumptionDao.saveRepairSpareConsumption(consumption);
		}
	}
}
